#!/usr/bin/env python
# -*- coding: UTF-8 -*-
"""
@Project : gtmsAutoTestProject 
@File    : shop_table_code_server.py
@IDE     : PyCharm 
@Author  : Mario
@Date    : 2022/8/23 15:22 
@Describe: 获取门店桌位码业务实现
"""
import os
from Config.setting import ConfigHandler
from Comm.logUtils.logControl import LogHandler
from Comm.tools.toolsControl import ToolsHandler
from APIs.posApi.table.table_api import TableApi
from APIs.middleApi.operate.shop.shop_api import ShopApi
from FastApi.enum.Interface_enum.error_enum import ERROR_ENUM

logger = LogHandler().get_logger()


def table_code_delete():
    """
    运行前先删除文件夹图片
    :return:
    """
    code_path = ConfigHandler.PHOTO_FILE_PATH
    logger.debug("获取桌码信息前先删除原来文件夹数据中~删除路径:[{}]".format(code_path))
    # 运行前调用【delete_specify_format_document】方法先删除文件夹图片,防止文件夹数据过多情况
    ToolsHandler.delete_specify_format_document(code_path, '.png')


def table_code_file_save(file_url):
    """
    文件保存到本地
    :param file_url: 下载URL
    :return:
    """
    file_path = ConfigHandler.TABLE_CODE_PATH
    logger.debug("正在保存获取的桌码数据中~保存路径及名称:[{}]".format(file_path))
    ToolsHandler.load_with_url(file_url, file_path)


class ShopTableCodeServer:
    def __init__(self, brandCode, environment):
        """
        构造函数
        :param brandCode: 品牌编码
        :param environment: 环境
        """
        self.brandCode = brandCode
        self.table_api = TableApi(brandCode, environment)
        self.shop_api = ShopApi(environment)

    def get_table_code_url(self, table_code) -> bool:
        """
        获取中台台位号下载地址
        :param table_code: 台位编码
        :return: 保存结果
        """
        response = self.shop_api.get_shop_table_code(self.brandCode, table_code)
        table_code_url = ToolsHandler.get_json_result(response, "$..qrcodeFileUrl")
        table_code_file_save(table_code_url)
        # 判断文件是否保存成功
        if os.path.exists(ConfigHandler.TABLE_CODE_PATH):
            return True
        else:
            return False

    def get_pos_idle_table(self):
        """
        获取当前环境品牌的POS空闲台位
        :return:
        """
        table_code_delete()
        table_list = self.table_api.table_list("0")
        idle_table_list = ToolsHandler.get_json_result(table_list, '$..data[?(@.status==0)]')
        if idle_table_list:
            idle_table_code = idle_table_list[0]['tableCode']
            save_result = self.get_table_code_url(idle_table_code)
            if save_result:
                table_code_path = ConfigHandler.TABLE_CODE_PATH
                return table_code_path
            else:
                error_message = {
                    "code": ERROR_ENUM.TABLE_CODE_SAVING_FAILED.value[0],
                    "message": ERROR_ENUM.TABLE_CODE_SAVING_FAILED.value[1]
                }
                logger.error(ERROR_ENUM.TABLE_CODE_SAVING_FAILED.value[1])
                return error_message
        else:
            warning_message = {
                "code": ERROR_ENUM.THERE_IS_NO_FREE_TABLE.value[0],
                "message": ERROR_ENUM.THERE_IS_NO_FREE_TABLE.value[1]
            }
            logger.warning(ERROR_ENUM.THERE_IS_NO_FREE_TABLE.value[1])
            return warning_message
